﻿using System;
using br.com.SistemaChamados.Constants;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Model;
using System.Web.Security;
using br.com.SistemaChamados.Controller.Common;
using br.com.SistemaChamados.Util;
using System.Web.UI.WebControls;

public partial class Pages_StateUpdate : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ViewStateUserKey = Session.SessionID;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        br.com.SistemaChamados.Model.Common.User loggedUser = new br.com.SistemaChamados.Model.Common.User();
        UserController userController = new UserController();
        if (Session[UserConstants.SESSION_USER_LOGGED_IN] != null)
        {
            loggedUser.Id = Convert.ToInt32(Session[UserConstants.SESSION_USER_LOGGED_IN]);
            loggedUser = userController.GetUser(loggedUser);
            this.MasterPageFile = MasterPageDefiner.defineMasterPageByAccessRole(loggedUser.Profile);
            ((Label)Master.FindControl("lblUser")).Text = loggedUser.Name;
            this.Title = "SysCall - Sistema de chamados [logado como " + loggedUser.Name + "]";
        }
        else
        {
            Response.Redirect("~/Pages/Login/Login.aspx");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.IsInRole("ADMIN"))
        {
            if (!IsPostBack)
            {
                string stateId = Session[StateConstants.SESSION_STATE_ID].ToString();
                State state = new State();
                state.Id = Convert.ToInt32(stateId);
                StateController Controller = new StateController();
                State returnedState = new State();
                try
                {
                    returnedState = Controller.GetState(state.Id);
                    if (returnedState != null)
                    {
                        txbName.Text = returnedState.Name;
                        txbAcronym.Text = returnedState.Acronym;
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(@"<script language='javascript'>alert('Ocorreu um erro na carga da tela: <br>" + ex.Message +
                                       ".');</script>");
                }
            }
        }
        else
        {
            FormsAuthentication.SignOut();
            Response.Clear();
            Response.Redirect("~/Pages/Login/Login.aspx");
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        State stateUpdate = new State();
        StateController Controller = new StateController();
        try
        {
            if (Controller.UpdateState(populateObject(stateUpdate)) == EnumGenericState.GENERIC_STATE_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Estado corrigido com sucesso!');</script>");
                ClearFields();
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante a correção, por favor tente novamente!');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro na correção: <br>" + ex.Message +
                               ".');</script>");
            stateUpdate = null;
        }
        finally
        {
            Response.Redirect("StateMain.aspx");
            Session.Remove(StateConstants.SESSION_STATE_ID);
        }
    }

    private State populateObject(State stateUpdate)
    {
        string code = Session[StateConstants.SESSION_STATE_ID].ToString();
        stateUpdate.Id = Convert.ToInt32(code);
        stateUpdate.Name = txbName.Text;
        stateUpdate.Acronym = txbAcronym.Text;
        return stateUpdate;
    }

    public void ClearFields()
    {
        txbAcronym.Text = "";
        txbName.Text = "";
        txbName.Focus();
    }

}